<?php

namespace App\Http\Requests\Api;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class CompanyRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'comName' => 'required|max:240',
            'project' => 'required',
            'province' => 'required',
            'city' => 'required',
            'district' => 'required',
            'street' => 'required',
            'address' =>'required|max:100',
            'postcode' => 'regex:/^[0-9]{6}$/',
            'leader' => 'max:20',
            'leaderPhone' => 'regex:/^[1-9][0-9]{4,}$/',
            'accompany' => 'max:240',
            'workersNum' => 'integer|min:0',
            'material' => 'max:255',
            'product' => 'max:255',
            'assets' => 'max:255',
            'income' => 'numeric|min:0',
            'space' => 'numeric|min:0',
            'electrician' => 'integer|between:1,2',
            'weld' => 'integer|between:1,2',
            'highwork' => 'integer|between:1,2',
            'refrigeration' => 'integer|between:1,2',
            'else' => 'max:255',
            'boiler' => 'integer|min:0',
            'kongyaji' => 'integer|min:0',
            'qiping' => 'integer|min:0',
            'ylgd' => 'integer|min:0',
            'elevator' => 'integer|min:0',
            'qzjx' => 'integer|min:0',
            'jdcl' => 'integer|min:0',
            'yhqgpfifteen' => 'integer|min:0',
            'yhqgpfifty' => 'integer|min:0',
            'trqgd' => 'integer|between:1,2',
            'cjrl' => 'integer|between:1,2',
            'photo' => 'required',
            'license' => 'required',
        ];
    }

    public function messages()
    {
        return [
            'comName.required' => '公司名称不能为空',
            'comName.max' => '公司名称长度不能超过240个字符',
            'project.required' => '请选择项目名称',
            'province.required' => '请选择省份',
            'city.required' => '请选择城市',
            'district.required' => '请选择区',
            'street.required' => '请选择街道',
            'address.required' => '请输入街道、门牌号等信息',
            'address.max' => '街道、门牌号等信息长度不能超过100个字符',
            'postcode.regex' => '邮政编码必须是六位数字',
            'leader.max' => '法人姓名不能超过20个字符',
            'leaderPhone.regex'=>'法定代表人电话格式不正确',
            'accompany.max' => '企业陪同人员不能超过240个字符',
            'workersNum.integer' => '员工人数必须是数字',
            'workersNum.min' => '员工人数最小是0',
            'material.max' => '主要原料长度不能超过255个字符',
            'product.max' => '主要产品长度不能超过255个字符',
            'assets.max' => '固定资产长度不能超过255个字符',
            'income.numeric' => '收入必须是数字',
            'income.min' => '收入最少是0',
            'space.numeric' => '占地面积必须是数字',
            'space.min' => '占地面积最小是0',
            'electrician.integer' => '电工作业传递参数不正确',
            'electrician.between' => '电工作业传递参数不正确',
            'weld.integer' => '焊接与热切割作业传递参数不正确',
            'weld.between' => '焊接与热切割作业传递参数不正确',
            'highwork.integer' => '高处作业传递参数不正确',
            'highwork.between' => '高处作业传递参数不正确',
            'refrigeration.integer' => '制冷与空调传递参数不正确',
            'refrigeration.between' => '制冷与空调传递参数不正确',
            'else.max' => '其它长度不能超过255个字符',
            'boiler.integer' => '锅炉参数不正确',
            'boiler.min' => '锅炉数量最小为0',
            'kongyaji.integer' => '空压机参数不正确',
            'kongyaji.min' => '空压机数量最小为0',
            'qiping.integer' => '气瓶参数不正确',
            'qiping.min' => '气瓶数量最小为0',
            'ylgd.integer' => '压力管道参数不正确',
            'ylgd.min' => '压力数量最小为0',
            'elevator.integer' => '电梯参数不正确',
            'elevator.min' => '电梯数量最小为0',
            'qzjx.integer' => '起重机械参数不正确',
            'qzjx.min' => '起重机械数量最小为0',
            'jdcl.integer' => '场(厂)内专用机动车辆参数不正确',
            'jdcl.min' => '场(厂)内专用机动车辆数量最小为0',
            'yhqgpfifteen.integer' => '液化气钢瓶15kg数量参数不正确',
            'yhqgpfifteen.min' => '液化气钢瓶15kg数量最小为0',
            'yhqgpfifty.integer' => '液化气钢瓶50kg数量参数不正确',
            'yhqgpfifty.min' => '液化气钢瓶50kg数量最小为0',
            'trqgd.integer' => '天然气管道传递参数不正确',
            'trqgd.between' => '天然气管道传递参数不正确',
            'cjrl.integer' => '醇基燃料传递参数不正确',
            'cjrl.between' => '醇基燃料传递参数不正确',
            'photo.required' => '门头照片不能为空',
            'license.required' => '营业执照不能为空',

        ];
    }

    protected function failedValidation(Validator $validator) {
        $error= $validator->errors()->all();
        throw new HttpResponseException(response()->json(['message'=>$error[0],'code'=>'400'], 400));
    }
}
